//
//  LSYReadModel.m
//  LSYReader
//
//  Created by Labanotation on 16/5/31.
//  Copyright © 2016年 okwei. All rights reserved.
//
#import "NSString+EV.h"
#import "LSYFileUtil.h"
#import "LSYReadModel.h"

@implementation LSYReadModel
-(instancetype)initWithContent:(NSString *)content
{
    self = [super init];
    if (self) {
        _content = content;
        NSMutableArray *charpter = [NSMutableArray array];
        [LSYReadUtilites separateChapter:&charpter content:content];
        _chapters = charpter;
        _notes = [NSMutableArray array];
        _marks = [NSMutableArray array];
        _record = [[LSYRecordModel alloc] init];
        _record.chapterModel = charpter.firstObject;
        _record.chapterCount = _chapters.count;
        _marksRecord = [NSMutableDictionary dictionary];
        _type = ReaderTxt;
    }
    return self;
}
-(instancetype)initWithePub:(NSString *)ePubPath;
{
    self = [super init];
    if (self) {
        _chapters = [LSYReadUtilites ePubFileHandle:ePubPath];
        _notes = [NSMutableArray array];
        _marks = [NSMutableArray array];
        _record = [[LSYRecordModel alloc] init];
        _record.chapterModel = _chapters.firstObject;
        _record.chapterCount = _chapters.count;
        _marksRecord = [NSMutableDictionary dictionary];
        _type = ReaderEpub;
    }
    return self;
}
// online book
-(instancetype)initWithNetBook:(LSYBook *)netBook {
    self = [super init];
    if (self) {
        _chapters = [LSYReadUtilites netBookChapters:netBook];
        _notes = [NSMutableArray array];
        _marks = [NSMutableArray array];
        _record = [[LSYRecordModel alloc] init];
        _record.chapterModel = _chapters.firstObject;
        _record.chapterCount = _chapters.count;
        _marksRecord = [NSMutableDictionary dictionary];
        _title = netBook.bookName;
        _netBook = netBook;
        _resourcePath = [LSYFileUtil bookPath:netBook];
        _type = ReaderNet;
    }
    return self;
}
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.title forKey:@"title"];
    [aCoder encodeObject:self.content forKey:@"content"];
    [aCoder encodeObject:self.marks forKey:@"marks"];
    [aCoder encodeObject:self.notes forKey:@"notes"];
    [aCoder encodeObject:self.chapters forKey:@"chapters"];
    [aCoder encodeObject:self.record forKey:@"record"];
    [aCoder encodeObject:self.resourcePath forKey:@"resourcePath"];
    [aCoder encodeObject:self.marksRecord forKey:@"marksRecord"];
    [aCoder encodeObject:self.netBook forKey:@"netBook"];
    [aCoder encodeObject:@(self.type) forKey:@"type"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.content = [aDecoder decodeObjectForKey:@"content"];
        self.marks = [aDecoder decodeObjectForKey:@"marks"];
        self.notes = [aDecoder decodeObjectForKey:@"notes"];
        self.chapters = [aDecoder decodeObjectForKey:@"chapters"];
        self.record = [aDecoder decodeObjectForKey:@"record"];
        self.resourcePath = [aDecoder decodeObjectForKey:@"resourcePath"];
        self.marksRecord = [aDecoder decodeObjectForKey:@"marksRecord"];
        self.netBook = [aDecoder decodeObjectForKey:@"netBook"];
        self.type = [[aDecoder decodeObjectForKey:@"type"] integerValue];
    }
    return self;
}
-(NSURL *)resourceUrl {
    return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",[LSYFileUtil documentPath],self.resourcePath]];
}

-(NSString *)percentStrig {
    CGFloat percent = (CGFloat)self.record.chapter/self.record.chapterCount;
    CGFloat chapterPer = 0.f;
    if (self.record.chapterModel.pageCount > 1 ) {
        chapterPer = (CGFloat)self.record.page/((self.record.chapterModel.pageCount-1) *self.record.chapterCount);
    }
    return [NSString stringWithFormat:@"已读%0.2f%@",(percent+chapterPer)*100,@"%"];
}

- (BOOL)isEqual:(id)object {
    LSYReadModel *model = (LSYReadModel *)object;
    if ([self.netBook.bookName isEqualToString:model.netBook.bookName] && [self.netBook.bookAuthor isEqualToString:model.netBook.bookAuthor]) {
        return YES;
    }
    return NO;
}

-(void)updateReadModel:(LSYReadModel *)model {
    _chapters = model.chapters;
    _notes = [NSMutableArray array];
    _marks = [NSMutableArray array];
    _record = [[LSYRecordModel alloc] init];
    _record.chapterModel = model.record.chapterModel;
    _record.chapter      = model.record.chapter;
    _record.chapterCount = _chapters.count;
    _marksRecord = [NSMutableDictionary dictionary];
    _title = model.title;
    _netBook = model.netBook;
    _resourcePath = [LSYFileUtil bookPath:_netBook];
    _type = model.type;
}

+(void)updateLocalModel:(LSYReadModel *)readModel url:(NSURL *)url
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSString *key = [url.path lastPathComponent];
        NSMutableData *data=[[NSMutableData alloc]init];
        NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
        [archiver encodeObject:readModel forKey:key];
        [archiver finishEncoding];
        [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
    });
}
+(id)getLocalModelWithPath:(NSString *)path
{
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",[LSYFileUtil documentPath],path]];
    NSString *key = [url.path lastPathComponent];
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    if (!data) {
        if ([[key pathExtension] isEqualToString:@"txt"]) {
            LSYReadModel *model = [[LSYReadModel alloc] initWithContent:[LSYReadUtilites encodeWithURL:url]];
            model.resourcePath = path;
            [LSYReadModel updateLocalModel:model url:url];
            return model;
        }
        else if ([[key pathExtension] isEqualToString:@"epub"]){
            NSLog(@"this is epub");
            LSYReadModel *model = [[LSYReadModel alloc] initWithePub:url.path];
            model.resourcePath = path;
            [LSYReadModel updateLocalModel:model url:url];
            return model;
        }
        else{
            @throw [NSException exceptionWithName:@"FileException" reason:@"文件格式错误" userInfo:nil];
        }
        
    }
    NSKeyedUnarchiver *unarchive = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
    //主线程操作
    LSYReadModel *model = [unarchive decodeObjectForKey:key];
    return model;
}

- (void)dealloc {
    NSLog(@"===");
}
@end
